package com.ossjk.qlh.supplier.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ossjk.config.jwttoken.JwtTokenUtil;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.supplier.entity.Supplier;
import com.ossjk.qlh.supplier.service.ISupplierService;
import com.ossjk.qlh.supplier.vo.SupplierVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * Copyright  2022-01-10 QLH. Tech Ltd. All rights reserved.
 * 
 * @Package: com.ossjk.qlh.supplier.controller
 * @ClassName: SupplierController
 * @Description: -控制器
 * @author: DengJing
 * @date:  2022-01-10 09:54:42 
 */
@Api(tags = "")
@RestController
@RequestMapping("/supplier")
public class SupplierController extends BaseController {
	
	@Autowired
	private ISupplierService iSupplierService;

	@Autowired
	private JwtTokenUtil jwtTokenUtil;

	@ApiOperation(value = "列表")
	@RequiresPermissions("0025000")
	@GetMapping("/list")
	public ResponseBean<Page<SupplierVo>> list(Page page, @RequestParam(name ="key", required = false) String key, HttpServletRequest request) {
		QueryWrapper<Supplier> queryWrapper = new QueryWrapper<Supplier>();

		//角色限制
		if(!"1".equals(jwtTokenUtil.getUserIdFromToken(getToekn(request)))){
			queryWrapper.eq("s.crer",jwtTokenUtil.getUserIdFromToken(getToekn(request)));
		}
		//字段搜索
		if(StrUtil.isNotBlank(key)){
			queryWrapper.and(
					wrapper ->
			wrapper.like("s.name",key).
					or().like("business",key).
					or().like("charge_person",key).
					or().like("phone_number",key));
		}
		return ResponseBean.Success(iSupplierService.getSupplierList(page,queryWrapper));
	}

	@ApiOperation(value = "添加")
	@RequiresPermissions("0025005")
	@PostMapping("/save")
	public ResponseBean save(@RequestBody Supplier record) {
		if (iSupplierService.save(record)) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}

	@ApiOperation(value = "去编辑")
	@RequiresPermissions("0025010")
	@GetMapping("/toUpdate")
	public ResponseBean<Supplier> toUpdate(@ApiParam(value = "id") @RequestParam(name = "id") String id) {
		Supplier supplier = iSupplierService.getById(id);
		if (ObjectUtil.isNotNull(supplier)) {
			return ResponseBean.Success(supplier);
		} else {
			return ResponseBean.Fail();
		}
	}


	@ApiOperation(value = "编辑")
	@RequiresPermissions("0025015")
	@PutMapping("/update")
	public ResponseBean update(@RequestBody Supplier record) {
		if (iSupplierService.updateById(record)) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}

	@ApiOperation(value = "删除")
	@RequiresPermissions("0025020")
	@DeleteMapping("/remove")
	public ResponseBean remove(@ApiParam(value = "ids") @RequestParam(name = "ids") String[] ids) {
		if (iSupplierService.removeByIds(Arrays.asList(ids))) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}

	@ApiOperation(value = "评级")
	@RequiresPermissions("0025025")
	@PutMapping("/rate")
	public ResponseBean rate(@RequestBody Supplier record) {
		if (iSupplierService.updateById(record)) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}
}
